package projetolp2.GraphicInterface;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import projetolp2.Sistem.Sistema;

public class OpinioesConsultaUsuario extends MealPoints implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JPanel panelMenu = new JPanel(new BorderLayout());
	private JPanel panel1= new JPanel(new GridLayout(0, 1));
	private JPanel panel2= new JPanel(new GridLayout(0, 1));
	private JPanel panel3= new JPanel(new GridLayout(0, 1));
	private JPanel panel4 = new JPanel(new GridLayout(0, 1));
	private JComboBox ESTABELECIMENTOS= new JComboBox();
	private String usuario;
	private JLabel label;
	private JButton VOLTAR = new JButton(NomeBotoes.values()[idioma(IDIOMAPROGRAMA )].getVoltar());
	private JButton CONSULTAR = new JButton(NomeBotoes.values()[idioma(IDIOMAPROGRAMA )].getConsultar());
	
	JPanel painel = new JPanel(new GridLayout(0, 1));
	private JButton CONTINUAR = new JButton(NomeBotoes.values()[idioma(IDIOMAPROGRAMA )].getVoltar());
	private JFrame OPINIAOUSUARIO = new JFrame();

	private JPanel panelFinal = new JPanel();
	
	public OpinioesConsultaUsuario() {
		
	}
	@SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == VOLTAR){
			removerAdicionar(((OpinioesConsultaUsuario) consultaUsuario).panel(),visualizarOpinioes);
		}
		else if (e.getSource() == CONSULTAR){
			janela.disable();
			String opiniao = "ERRO";
			try {
				List<String> listaDeEstabelecimentos = SISTEMA.nomeDeEstabelecimentos();
				for (int i = 0;i<listaDeEstabelecimentos.size();i++){
					if(listaDeEstabelecimentos.get(i).equals(ESTABELECIMENTOS.getSelectedItem().toString()));
						opiniao = SISTEMA.OpiniaoUsuario(usuario, ESTABELECIMENTOS.getSelectedItem().toString());
				}
			} catch (Exception i) {
				i.printStackTrace();
			}
			panelRetornaOpinioes(usuario,ESTABELECIMENTOS.getSelectedItem().toString(),opiniao);
		}
		else if (e.getSource() == CONTINUAR){
			janela.enable();
			OPINIAOUSUARIO.dispose();
		}
	}
	private void ajudaDeBotoes(){
		VOLTAR.setToolTipText("Voltar a janela anterior");
		CONSULTAR.setToolTipText("Mostra a opiniao sobre o estabelecimento acima selecionado");
		CONTINUAR.setToolTipText("Voltar a janela anterior");
	}

	private void createComponents() {
		ajudaDeBotoes();
		
		try {
			List<String> listaEstabelecimentos = SISTEMA.nomeDeEstabelecimentos();
			for (int i = 0;i<listaEstabelecimentos.size();i++){
				ESTABELECIMENTOS.addItem(listaEstabelecimentos.get(i));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
        
		label = new JLabel("Opinioes de: "+usuario);
		
		panelMenu.removeAll();
		
        VOLTAR.setMnemonic(KeyEvent.VK_I);
        VOLTAR.addActionListener(this);
        label.setLabelFor(VOLTAR);
        
        CONSULTAR.setMnemonic(KeyEvent.VK_I);
        CONSULTAR.addActionListener(this);
        label.setLabelFor(CONSULTAR);
        
        panel1.add(label);
        panel2.add(ESTABELECIMENTOS);
        panel3.add(CONSULTAR);
        panel4.add(VOLTAR);
        panel1.setBorder(BorderFactory.createEmptyBorder(
        		10, //top
                10, //left
                10, //bottom
                10) //right
                );
        panel2.setBorder(BorderFactory.createEmptyBorder(
        		10, //top
                10, //left
                10, //bottom
                10) //right
                );
        panel3.setBorder(BorderFactory.createEmptyBorder(
                10, //top
                10, //left
                10, //bottom
                10) //right
                );
        panel4.setBorder(BorderFactory.createEmptyBorder(
        		10, //top
                10, //left
                10, //bottom
                10) //right
                );
        panelFinal.add(panel2);
        panelFinal.add(panel3);
        panelFinal.add(panel4);
        panelMenu.add(panelFinal,BorderLayout.NORTH);
        panelMenu.validate();
        panelMenu.repaint();
    }	
	
	public Component panel(){
		createComponents();
		panelMenu.setVisible(true);
		
		
		return panelMenu;
	}
	public void usuarioAConsultar(String usuario) {
		this.usuario = usuario;		
	}
	private void panelRetornaOpinioes(String usuario, String estabelecimento,String opiniao){
		painel.removeAll();
		JLabel USUARIO = new JLabel("Usuario(a): "+usuario);
		JLabel ESTABELECIMENTO = new JLabel(estabelecimento);
		JLabel OPINIAO = new JLabel("OPINIAO: "+opiniao.split(":")[1]);
		OPINIAOUSUARIO.setSize(320,200);//Tamanho da janela
		OPINIAOUSUARIO.setResizable(false);//nao pode redimensionar a janela
		OPINIAOUSUARIO.setLocation(30,30);//Local onde a janela aparecera
		OPINIAOUSUARIO.setIconImage(null);

		OPINIAOUSUARIO.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		OPINIAOUSUARIO.addWindowListener(new java.awt.event.WindowAdapter() {   
			@SuppressWarnings("deprecation")
			public void windowClosing(java.awt.event.WindowEvent e) { 
				if (e.getID() == WindowEvent.WINDOW_CLOSING){   
					OPINIAOUSUARIO.dispose();
					janela.enable(true);
				}
			}
		});
		
		CONTINUAR.setMnemonic(KeyEvent.VK_I);
		CONTINUAR.addActionListener(this);
        label.setLabelFor(CONTINUAR);
		
        painel.add(USUARIO);
        painel.add(ESTABELECIMENTO);
        painel.add(OPINIAO);
        painel.add(CONTINUAR);
        painel.setBorder(BorderFactory.createEmptyBorder(
                15, //top
                40, //left
                15, //bottom
                40) //right
        );
		
        OPINIAOUSUARIO.add(painel);
		OPINIAOUSUARIO.setVisible(true);
		
	}
}
